<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Cookbook &raquo; Conditional Tasking | Taskflow QuickStart</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
  <link rel="stylesheet" href="m-dark+documentation.compiled.css" />
  <link rel="icon" href="favicon.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="theme-color" content="#22272e" />
</head>
<body>
<header><nav id="navigation">
  <div class="m-container">
    <div class="m-row">
      <span id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">
        <a href="https://taskflow.github.io"><img src="taskflow_logo.png" alt="" />Taskflow</a> <span class="m-breadcrumb">|</span> <a href="index.html" class="m-thin">QuickStart</a>
      </span>
      <div class="m-col-t-4 m-hide-m m-text-right m-nopadr">
        <a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
          <path id="m-doc-search-icon-path" d="m6 0c-3.31 0-6 2.69-6 6 0 3.31 2.69 6 6 6 1.49 0 2.85-0.541 3.89-1.44-0.0164 0.338 0.147 0.759 0.5 1.15l3.22 3.79c0.552 0.614 1.45 0.665 2 0.115 0.55-0.55 0.499-1.45-0.115-2l-3.79-3.22c-0.392-0.353-0.812-0.515-1.15-0.5 0.895-1.05 1.44-2.41 1.44-3.89 0-3.31-2.69-6-6-6zm0 1.56a4.44 4.44 0 0 1 4.44 4.44 4.44 4.44 0 0 1-4.44 4.44 4.44 4.44 0 0 1-4.44-4.44 4.44 4.44 0 0 1 4.44-4.44z"/>
        </svg></a>
        <a id="m-navbar-show" href="#navigation" title="Show navigation"></a>
        <a id="m-navbar-hide" href="#" title="Hide navigation"></a>
      </div>
      <div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
        <div class="m-row">
          <ol class="m-col-t-6 m-col-m-none">
            <li><a href="pages.html">Handbook</a></li>
            <li><a href="namespaces.html">Namespaces</a></li>
          </ol>
          <ol class="m-col-t-6 m-col-m-none" start="3">
            <li><a href="annotated.html">Classes</a></li>
            <li><a href="files.html">Files</a></li>
            <li class="m-show-m"><a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
              <use href="#m-doc-search-icon-path" />
            </svg></a></li>
          </ol>
        </div>
      </div>
    </div>
  </div>
</nav></header>
<main><article>
  <div class="m-container m-container-inflatable">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <h1>
          <span class="m-breadcrumb"><a href="Cookbook.html">Cookbook</a> &raquo;</span>
          Conditional Tasking
        </h1>
        <nav class="m-block m-default">
          <h3>Contents</h3>
          <ul>
            <li><a href="#CreateAConditionTask">Create a Condition Task</a></li>
            <li>
              <a href="#TaskSchedulingPolicy">Understand our Task-level Scheduling</a>
              <ul>
                <li><a href="#TaskLevelSchedulingExample">Example</a></li>
              </ul>
            </li>
            <li><a href="#AvoidCommonPitfalls">Avoid Common Pitfalls</a></li>
            <li>
              <a href="#ImplementControlFlowGraphs">Implement Control-flow Graphs</a>
              <ul>
                <li><a href="#ImplementIfElseControlFlow">Implement If-Else Control Flow</a></li>
                <li><a href="#ImplementSwitchControlFlow">Implement Switch Control Flow</a></li>
                <li><a href="#ImplementDoWhileLoopControlFlow">Implement Do-While-Loop Control Flow</a></li>
                <li><a href="#ImplementWhileLoopControlFlow">Implement While-Loop Control Flow</a></li>
              </ul>
            </li>
            <li><a href="#CreateAMultiConditionTask">Create a Multi-condition Task</a></li>
          </ul>
        </nav>
<p>Parallel workloads often require making control-flow decisions across dependent tasks. Taskflow supports an very efficient interface of conditional tasking for users to implement general control flow such as dynamic flow, cycles, and conditionals that are otherwise difficult to do with existing frameworks.</p><section id="CreateAConditionTask"><h2><a href="#CreateAConditionTask">Create a Condition Task</a></h2><p>A condition task evaluates a set of instructions and returns an integer index of the next successor task to execute. The index is defined with respect to the order of its successor construction. The following example creates an if-else block using a single condition task.</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">init</span><span class="p">,</span><span class="w"> </span><span class="n">cond</span><span class="p">,</span><span class="w"> </span><span class="n">yes</span><span class="p">,</span><span class="w"> </span><span class="n">no</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w">  </span><span class="p">[]</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w">  </span><span class="p">[]</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w">  </span><span class="p">[]</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;yes</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w">  </span><span class="p">[]</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;no</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="p">);</span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="n">cond</span><span class="p">.</span><span class="n">succeed</span><span class="p">(</span><span class="n">init</span><span class="p">)</span>
<span class="mi">11</span><span class="o">:</span><span class="w">     </span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">yes</span><span class="p">,</span><span class="w"> </span><span class="n">no</span><span class="p">);</span><span class="w">  </span><span class="c1">// executes yes if cond returns 0</span>
<span class="mi">12</span><span class="o">:</span><span class="w">                         </span><span class="c1">// executes no  if cond returns 1</span></pre><div class="m-graph"><svg style="width: 25.600rem; height: 9.800rem;" viewBox="0.00 0.00 256.17 98.00">
<g transform="scale(1 1) rotate(0) translate(4 94)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7f9e1e700030</title>
<ellipse cx="27" cy="-45" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-41.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7f9e1e700140</title>
<polygon points="121.96,-63 91,-45 121.96,-27 152.92,-45 121.96,-63"/>
<text text-anchor="middle" x="121.96" y="-41.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-edge">
<title>p0x7f9e1e700030&#45;&gt;p0x7f9e1e700140</title>
<path d="M54.39,-45C61.98,-45 70.45,-45 78.74,-45"/>
<polygon points="78.72,-48.5 88.72,-45 78.72,-41.5 78.72,-48.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7f9e1e700250</title>
<ellipse cx="221.17" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="221.17" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">yes</text>
</g>
<g class="m-edge">
<title>p0x7f9e1e700140&#45;&gt;p0x7f9e1e700250</title>
<path stroke-dasharray="5,2" d="M143.58,-50.73C155.63,-54.07 171.16,-58.39 185.08,-62.25"/>
<polygon points="183.78,-65.52 194.35,-64.83 185.65,-58.78 183.78,-65.52"/>
<text text-anchor="middle" x="173.55" y="-60.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7f9e1e700360</title>
<ellipse cx="221.17" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="221.17" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">no</text>
</g>
<g class="m-edge">
<title>p0x7f9e1e700140&#45;&gt;p0x7f9e1e700360</title>
<path stroke-dasharray="5,2" d="M143.58,-39.27C155.63,-35.93 171.16,-31.61 185.08,-27.75"/>
<polygon points="185.65,-31.22 194.35,-25.17 183.78,-24.48 185.65,-31.22"/>
<text text-anchor="middle" x="173.55" y="-32.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
</g>
</svg>
</div><p>Line 5 creates a condition task <code>cond</code> and line 11 creates two dependencies from <code>cond</code> to two other tasks, <code>yes</code> and <code>no</code>. With this order, when <code>cond</code> returns 0, the execution moves on to task <code>yes</code>. When <code>cond</code> returns 1, the execution moves on to task <code>no</code>.</p><aside class="m-note m-warning"><h4>Attention</h4><p>It is your responsibility to ensure the return of a condition task goes to a correct successor task. If the return falls beyond the range of the successors, the executor will not schedule any tasks.</p></aside><p>Condition task can go cyclic to describe <em>iterative</em> control flow. The example below implements a simple yet commonly used feedback loop through a condition task (line 7-10) that returns a random binary value. If the return value from <code>cond</code> is <code>0</code>, it loops back to itself, or otherwise to <code>stop</code>.</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">init</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;init&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">stop</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;stop&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w"> </span><span class="c1">// creates a condition task that returns 0 or 1</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">cond</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w">   </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;flipping a coin</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w">   </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;cond&quot;</span><span class="p">);</span>
<span class="mi">11</span><span class="o">:</span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="c1">// creates a feedback loop {0: cond, 1: stop}</span>
<span class="mi">13</span><span class="o">:</span><span class="w"> </span><span class="n">init</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">);</span>
<span class="mi">14</span><span class="o">:</span><span class="w"> </span><span class="n">cond</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">,</span><span class="w"> </span><span class="n">stop</span><span class="p">);</span><span class="w">  </span><span class="c1">// returns 0 to &#39;cond&#39; or 1 to &#39;stop&#39;</span>
<span class="mi">15</span><span class="o">:</span>
<span class="mi">16</span><span class="o">:</span><span class="w"> </span><span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><div class="m-graph"><svg style="width: 25.600rem; height: 7.300rem;" viewBox="0.00 0.00 256.17 73.25">
<g transform="scale(1 1) rotate(0) translate(4 69.25)">
<title>Taskflow</title>
<g class="m-node">
<title>cond</title>
<polygon points="121.96,-36 91,-18 121.96,0 152.92,-18 121.96,-36"/>
<text text-anchor="middle" x="121.96" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-edge">
<title>cond&#45;&gt;cond</title>
<path stroke-dasharray="5,2" d="M113.99,-31.67C110.8,-42.66 113.46,-54 121.96,-54 127.41,-54 130.45,-49.34 131.1,-43.11"/>
<polygon points="134.59,-42.76 130.09,-33.17 127.62,-43.48 134.59,-42.76"/>
<text text-anchor="middle" x="121.96" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>stop</title>
<ellipse cx="221.17" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="221.17" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">stop</text>
</g>
<g class="m-edge">
<title>cond&#45;&gt;stop</title>
<path stroke-dasharray="5,2" d="M153.72,-18C162.89,-18 173.05,-18 182.61,-18"/>
<polygon points="182.35,-21.5 192.35,-18 182.35,-14.5 182.35,-21.5"/>
<text text-anchor="middle" x="173.55" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>init</title>
<ellipse cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-edge">
<title>init&#45;&gt;cond</title>
<path d="M54.39,-18C61.98,-18 70.45,-18 78.74,-18"/>
<polygon points="78.72,-21.5 88.72,-18 78.72,-14.5 78.72,-21.5"/>
</g>
</g>
</svg>
</div><p>A taskflow of complex control flow often just takes a few lines of code to implement, and different control flow blocks may run in parallel. The code below creates another taskflow with three condition tasks.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;A&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;B&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;C&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;D&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;E&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;F&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;G&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">H</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;H&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;I&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">K</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;K&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">L</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;L&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">M</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;M&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">cond_1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;cond_1&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">cond_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;cond_2&quot;</span><span class="p">);</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">cond_3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;cond_3&quot;</span><span class="p">);</span>

<span class="n">A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="p">);</span>
<span class="n">B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">C</span><span class="p">);</span>
<span class="n">C</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">D</span><span class="p">);</span>
<span class="n">D</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond_1</span><span class="p">);</span>
<span class="n">E</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">K</span><span class="p">);</span>
<span class="n">F</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond_2</span><span class="p">);</span>
<span class="n">H</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">I</span><span class="p">);</span>
<span class="n">I</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond_3</span><span class="p">);</span>
<span class="n">L</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">M</span><span class="p">);</span>

<span class="n">cond_1</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">E</span><span class="p">);</span><span class="w">       </span><span class="c1">// return 0 to &#39;B&#39; or 1 to &#39;E&#39;</span>
<span class="n">cond_2</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">G</span><span class="p">,</span><span class="w"> </span><span class="n">H</span><span class="p">);</span><span class="w">       </span><span class="c1">// return 0 to &#39;G&#39; or 1 to &#39;H&#39;</span>
<span class="n">cond_3</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond_3</span><span class="p">,</span><span class="w"> </span><span class="n">L</span><span class="p">);</span><span class="w">  </span><span class="c1">// return 0 to &#39;cond_3&#39; or 1 to &#39;L&#39;</span>

<span class="n">taskflow</span><span class="p">.</span><span class="n">dump</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="p">);</span></pre><p>The above code creates three condition tasks: (1) a condition task <code>cond_1</code> that loops back to <code>B</code> on returning <code>0</code>, or proceeds to <code>E</code> on returning <code>1</code>, (2) a condition task <code>cond_2</code> that goes to <code>G</code> on returning <code>0</code>, or <code>H</code> on returning <code>1</code>, (3) a condition task <code>cond_3</code> that loops back to itself on returning <code>0</code>, or proceeds to <code>L</code> on returning <code>1</code></p><div class="m-graph"><svg style="width: 78.800rem; height: 19.200rem;" viewBox="0.00 0.00 788.23 192.00">
<g transform="scale(1 1) rotate(0) translate(4 188)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fecbdc02830</title>
<ellipse cx="27" cy="-94" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-90.12" font-family="Helvetica,sans-Serif" font-size="10.00">A</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02940</title>
<ellipse cx="118" cy="-122" rx="27" ry="18"/>
<text text-anchor="middle" x="118" y="-118.12" font-family="Helvetica,sans-Serif" font-size="10.00">B</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02830&#45;&gt;p0x7fecbdc02940</title>
<path d="M51.85,-101.5C61.23,-104.45 72.18,-107.9 82.39,-111.11"/>
<polygon points="81.05,-114.36 91.64,-114.02 83.15,-107.68 81.05,-114.36"/>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02d80</title>
<ellipse cx="118" cy="-68" rx="27" ry="18"/>
<text text-anchor="middle" x="118" y="-64.12" font-family="Helvetica,sans-Serif" font-size="10.00">F</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02830&#45;&gt;p0x7fecbdc02d80</title>
<path d="M52.32,-86.9C61.44,-84.23 72,-81.14 81.91,-78.25"/>
<polygon points="82.64,-81.68 91.26,-75.52 80.68,-74.97 82.64,-81.68"/>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02a50</title>
<ellipse cx="221.75" cy="-166" rx="27" ry="18"/>
<text text-anchor="middle" x="221.75" y="-162.12" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02940&#45;&gt;p0x7fecbdc02a50</title>
<path d="M141.33,-131.66C154.98,-137.56 172.69,-145.22 187.91,-151.8"/>
<polygon points="186.51,-155.01 197.08,-155.77 189.29,-148.58 186.51,-155.01"/>
</g>
<g class="m-node">
<title>p0x7fecbdc03600</title>
<polygon points="221.75,-86 182,-68 221.75,-50 261.49,-68 221.75,-86"/>
<text text-anchor="middle" x="221.75" y="-64.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond_2</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02d80&#45;&gt;p0x7fecbdc03600</title>
<path d="M145.47,-68C152.92,-68 161.28,-68 169.65,-68"/>
<polygon points="169.47,-71.5 179.47,-68 169.47,-64.5 169.47,-71.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02b60</title>
<ellipse cx="329.74" cy="-166" rx="27" ry="18"/>
<text text-anchor="middle" x="329.74" y="-162.12" font-family="Helvetica,sans-Serif" font-size="10.00">D</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02a50&#45;&gt;p0x7fecbdc02b60</title>
<path d="M249.22,-166C261.96,-166 277.46,-166 291.38,-166"/>
<polygon points="291.09,-169.5 301.09,-166 291.09,-162.5 291.09,-169.5"/>
</g>
<g class="m-node">
<title>p0x7fecbdc034f0</title>
<polygon points="433.49,-155 393.74,-137 433.49,-119 473.24,-137 433.49,-155"/>
<text text-anchor="middle" x="433.49" y="-133.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond_1</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02b60&#45;&gt;p0x7fecbdc034f0</title>
<path d="M355.12,-159.05C367.83,-155.43 383.6,-150.94 397.58,-146.95"/>
<polygon points="398.21,-150.41 406.86,-144.3 396.29,-143.68 398.21,-150.41"/>
</g>
<g class="m-edge">
<title>p0x7fecbdc034f0&#45;&gt;p0x7fecbdc02940</title>
<path stroke-dasharray="5,2" d="M396.45,-135.28C336.62,-132.41 217.35,-126.71 156.54,-123.8"/>
<polygon points="156.86,-120.31 146.7,-123.33 156.52,-127.3 156.86,-120.31"/>
<text text-anchor="middle" x="282.12" y="-130.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02c70</title>
<ellipse cx="554.24" cy="-137" rx="27" ry="18"/>
<text text-anchor="middle" x="554.24" y="-133.12" font-family="Helvetica,sans-Serif" font-size="10.00">E</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc034f0&#45;&gt;p0x7fecbdc02c70</title>
<path stroke-dasharray="5,2" d="M474.36,-137C487.67,-137 502.45,-137 515.59,-137"/>
<polygon points="515.58,-140.5 525.58,-137 515.58,-133.5 515.58,-140.5"/>
<text text-anchor="middle" x="493.86" y="-138.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc031c0</title>
<ellipse cx="662.23" cy="-137" rx="27" ry="18"/>
<text text-anchor="middle" x="662.23" y="-133.12" font-family="Helvetica,sans-Serif" font-size="10.00">K</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02c70&#45;&gt;p0x7fecbdc031c0</title>
<path d="M581.71,-137C594.45,-137 609.95,-137 623.87,-137"/>
<polygon points="623.58,-140.5 633.58,-137 623.58,-133.5 623.58,-140.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02e90</title>
<ellipse cx="329.74" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="329.74" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">G</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc03600&#45;&gt;p0x7fecbdc02e90</title>
<path stroke-dasharray="5,2" d="M259.52,-69.39C269.67,-69.77 280.68,-70.19 290.89,-70.57"/>
<polygon points="290.75,-74.07 300.88,-70.95 291.01,-67.07 290.75,-74.07"/>
<text text-anchor="middle" x="282.12" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc02fa0</title>
<ellipse cx="329.74" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="329.74" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">H</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc03600&#45;&gt;p0x7fecbdc02fa0</title>
<path stroke-dasharray="5,2" d="M242.19,-58.83C257.5,-51.6 279.17,-41.38 297,-32.98"/>
<polygon points="298.32,-36.22 305.87,-28.79 295.33,-29.89 298.32,-36.22"/>
<text text-anchor="middle" x="282.12" y="-41.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc030b0</title>
<ellipse cx="433.49" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="433.49" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">I</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc02fa0&#45;&gt;p0x7fecbdc030b0</title>
<path d="M357.21,-18C368.63,-18 382.21,-18 394.66,-18"/>
<polygon points="394.64,-21.5 404.64,-18 394.64,-14.5 394.64,-21.5"/>
</g>
<g class="m-node">
<title>p0x7fecbdc03710</title>
<polygon points="554.24,-36 514.49,-18 554.24,0 593.98,-18 554.24,-36"/>
<text text-anchor="middle" x="554.24" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond_3</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc030b0&#45;&gt;p0x7fecbdc03710</title>
<path d="M460.85,-18C473.03,-18 487.93,-18 502.19,-18"/>
<polygon points="501.9,-21.5 511.9,-18 501.9,-14.5 501.9,-21.5"/>
</g>
<g class="m-edge">
<title>p0x7fecbdc03710&#45;&gt;p0x7fecbdc03710</title>
<path stroke-dasharray="5,2" d="M544.38,-32.04C540.69,-42.91 543.98,-54 554.24,-54 560.81,-54 564.52,-49.45 565.37,-43.32"/>
<polygon points="568.86,-43.09 564.26,-33.55 561.91,-43.88 568.86,-43.09"/>
<text text-anchor="middle" x="554.24" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc032d0</title>
<ellipse cx="662.23" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="662.23" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">L</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc03710&#45;&gt;p0x7fecbdc032d0</title>
<path stroke-dasharray="5,2" d="M595.01,-18C604.34,-18 614.26,-18 623.5,-18"/>
<polygon points="623.24,-21.5 633.24,-18 623.24,-14.5 623.24,-21.5"/>
<text text-anchor="middle" x="614.61" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fecbdc033e0</title>
<ellipse cx="753.23" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="753.23" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">M</text>
</g>
<g class="m-edge">
<title>p0x7fecbdc032d0&#45;&gt;p0x7fecbdc033e0</title>
<path d="M689.46,-18C697.35,-18 706.18,-18 714.66,-18"/>
<polygon points="714.38,-21.5 724.38,-18 714.38,-14.5 714.38,-21.5"/>
</g>
</g>
</svg>
</div><p>You can use condition tasks to create cycles as long as the graph does not introduce task race during execution. However, cycles are not allowed in non-condition tasks.</p><aside class="m-note m-info"><h4>Note</h4><p>Conditional tasking lets you make in-task control-flow decisions to enable <em>end-to-end</em> parallelism, instead of resorting to client-side partition or synchronizing your task graph at the decision points of control flow.</p></aside></section><section id="TaskSchedulingPolicy"><h2><a href="#TaskSchedulingPolicy">Understand our Task-level Scheduling</a></h2><p>In order to understand how an executor schedules condition tasks, we define two dependency types, <em>strong dependency</em> and <em>weak dependency</em>. A strong dependency is a preceding link from a non-condition task to another task. A weak dependency is a preceding link from a condition task to another task. The number of dependents of a task is the sum of strong dependency and weak dependency. The table below lists the strong dependency and weak dependency numbers of each task in the previous example.</p><table class="m-table"><thead><tr><th>task</th><th>strong dependency</th><th>weak dependency</th><th>dependents</th></tr></thead><tbody><tr><td>A</td><td>0</td><td>0</td><td>0</td></tr><tr><td>B</td><td>1</td><td>1</td><td>2</td></tr><tr><td>C</td><td>1</td><td>0</td><td>1</td></tr><tr><td>D</td><td>1</td><td>0</td><td>1</td></tr><tr><td>E</td><td>0</td><td>1</td><td>1</td></tr><tr><td>F</td><td>1</td><td>0</td><td>1</td></tr><tr><td>G</td><td>0</td><td>1</td><td>1</td></tr><tr><td>H</td><td>0</td><td>1</td><td>1</td></tr><tr><td>I</td><td>1</td><td>0</td><td>1</td></tr><tr><td>K</td><td>1</td><td>0</td><td>1</td></tr><tr><td>L</td><td>0</td><td>1</td><td>1</td></tr><tr><td>M</td><td>1</td><td>0</td><td>1</td></tr><tr><td>cond_1</td><td>1</td><td>0</td><td>1</td></tr><tr><td>cond_2</td><td>1</td><td>0</td><td>1</td></tr><tr><td>cond_3</td><td>1</td><td>1</td><td>2</td></tr></tbody></table><p>You can query the number of strong dependents, the number of weak dependents, and the number of dependents of a task.</p><pre class="m-code"><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>
<span class="mi">2</span><span class="o">:</span><span class="w"> </span>
<span class="mi">3</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">task</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){});</span>
<span class="mi">4</span><span class="o">:</span><span class="w"> </span>
<span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="c1">// ... add more tasks and preceding links</span>
<span class="mi">6</span><span class="o">:</span>
<span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">num_dependents</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="sc">&#39;\n&#39;</span><span class="p">;</span>
<span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">num_strong_dependents</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="sc">&#39;\n&#39;</span><span class="p">;</span><span class="w"> </span>
<span class="mi">9</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">task</span><span class="p">.</span><span class="n">num_weak_dependents</span><span class="p">()</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="sc">&#39;\n&#39;</span><span class="p">;</span></pre><p>When you submit a task to an executor, the scheduler starts with tasks of <em>zero dependents</em> (both zero strong and weak dependencies) and continues to execute successive tasks whenever their <em>strong dependencies</em> are met. However, the scheduler skips this rule when executing a condition task and jumps directly to its successors indexed by the return value.</p><div class="m-graph"><svg style="width: 55.800rem; height: 34.600rem;" viewBox="0.00 0.00 558.12 346.25">
<g transform="scale(1 1) rotate(0) translate(4 342.25)">
<title>G</title>
<g class="m-node m-flat">
<title>atask</title>
<ellipse cx="337.89" cy="-320.25" rx="30.77" ry="18"/>
<text text-anchor="middle" x="337.89" y="-316.38" font-family="Helvetica,sans-Serif" font-size="10.00">a task T</text>
</g>
<g class="m-node">
<title>cond</title>
<polygon points="337.89,-265.25 246.05,-247.25 337.89,-229.25 429.72,-247.25 337.89,-265.25"/>
<text text-anchor="middle" x="337.89" y="-243.38" font-family="Helvetica,sans-Serif" font-size="10.00">is T a condition task?</text>
</g>
<g class="m-edge">
<title>atask&#45;&gt;cond</title>
<path d="M337.89,-302.06C337.89,-294.48 337.89,-285.35 337.89,-276.79"/>
<polygon points="341.39,-276.79 337.89,-266.79 334.39,-276.79 341.39,-276.79"/>
</g>
<g class="m-node m-flat">
<title>invokeN</title>
<ellipse cx="289.89" cy="-164" rx="33.43" ry="18"/>
<text text-anchor="middle" x="289.89" y="-160.12" font-family="Helvetica,sans-Serif" font-size="10.00">invoke(T)</text>
</g>
<g class="m-edge">
<title>cond&#45;&gt;invokeN</title>
<path stroke-dasharray="5,2" d="M328.63,-230.58C322.06,-219.45 313.06,-204.23 305.4,-191.26"/>
<polygon points="308.48,-189.59 300.38,-182.76 302.45,-193.15 308.48,-189.59"/>
<text text-anchor="middle" x="322.14" y="-201.75" font-family="Helvetica,sans-Serif" font-size="10.00">no</text>
</g>
<g class="m-node m-flat">
<title>invokeY</title>
<ellipse cx="432.89" cy="-164" rx="44.97" ry="18"/>
<text text-anchor="middle" x="432.89" y="-160.12" font-family="Helvetica,sans-Serif" font-size="10.00">R = invoke(T)</text>
</g>
<g class="m-edge">
<title>cond&#45;&gt;invokeY</title>
<path stroke-dasharray="5,2" d="M354.44,-232.1C368.61,-219.97 389.3,-202.28 405.87,-188.11"/>
<polygon points="408.11,-190.8 413.43,-181.64 403.56,-185.48 408.11,-190.8"/>
<text text-anchor="middle" x="399.76" y="-201.75" font-family="Helvetica,sans-Serif" font-size="10.00">yes</text>
</g>
<g class="m-node m-flat">
<title>decrement</title>
<ellipse cx="171.89" cy="-91" rx="171.89" ry="18"/>
<text text-anchor="middle" x="171.89" y="-87.12" font-family="Helvetica,sans-Serif" font-size="10.00">decrement strong dependencies of each successor of T by one</text>
</g>
<g class="m-edge">
<title>invokeN&#45;&gt;decrement</title>
<path d="M268.24,-149.98C252.04,-140.23 229.45,-126.63 210.21,-115.06"/>
<polygon points="212.18,-112.16 201.81,-110 208.57,-118.16 212.18,-112.16"/>
</g>
<g class="m-node m-flat">
<title>enqueueR</title>
<ellipse cx="455.89" cy="-91" rx="94.23" ry="18"/>
<text text-anchor="middle" x="455.89" y="-87.12" font-family="Helvetica,sans-Serif" font-size="10.00">enqueue the R&#45;th successor of T</text>
</g>
<g class="m-edge">
<title>invokeY&#45;&gt;enqueueR</title>
<path d="M438.46,-145.81C440.97,-138.05 444.01,-128.68 446.83,-119.95"/>
<polygon points="450.15,-121.06 449.9,-110.47 443.49,-118.9 450.15,-121.06"/>
</g>
<g class="m-node m-flat">
<title>enqueueS</title>
<ellipse cx="171.89" cy="-18" rx="137.27" ry="18"/>
<text text-anchor="middle" x="171.89" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">enqueue successors of zero strong dependencies</text>
</g>
<g class="m-edge">
<title>decrement&#45;&gt;enqueueS</title>
<path d="M171.89,-72.81C171.89,-65.23 171.89,-56.1 171.89,-47.54"/>
<polygon points="175.39,-47.54 171.89,-37.54 168.39,-47.54 175.39,-47.54"/>
</g>
</g>
</svg>
</div><p>Each task has an <em>atomic</em> join counter to keep track of strong dependents that are met at runtime. When a task completes, the join counter is restored to the task&#x27;s strong dependency number in the graph, such that the subsequent execution can reuse the counter again.</p><section id="TaskLevelSchedulingExample"><h3><a href="#TaskLevelSchedulingExample">Example</a></h3><p>Let&#x27;s take a look at an example to understand how task-level scheduling works. Suppose we have the following taskflow of one condition task <code>cond</code> that forms a loop to itself on returning <code>0</code> and moves on to <code>stop</code> on returning <code>1</code>:</p><div class="m-graph"><svg style="width: 25.600rem; height: 7.300rem;" viewBox="0.00 0.00 256.17 73.25">
<g transform="scale(1 1) rotate(0) translate(4 69.25)">
<title>Taskflow</title>
<g class="m-node">
<title>cond</title>
<polygon points="121.96,-36 91,-18 121.96,0 152.92,-18 121.96,-36"/>
<text text-anchor="middle" x="121.96" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond</text>
</g>
<g class="m-edge">
<title>cond&#45;&gt;cond</title>
<path stroke-dasharray="5,2" d="M113.99,-31.67C110.8,-42.66 113.46,-54 121.96,-54 127.41,-54 130.45,-49.34 131.1,-43.11"/>
<polygon points="134.59,-42.76 130.09,-33.17 127.62,-43.48 134.59,-42.76"/>
<text text-anchor="middle" x="121.96" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>stop</title>
<ellipse cx="221.17" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="221.17" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">stop</text>
</g>
<g class="m-edge">
<title>cond&#45;&gt;stop</title>
<path stroke-dasharray="5,2" d="M153.72,-18C162.89,-18 173.05,-18 182.61,-18"/>
<polygon points="182.35,-21.5 192.35,-18 182.35,-14.5 182.35,-21.5"/>
<text text-anchor="middle" x="173.55" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>init</title>
<ellipse cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-edge">
<title>init&#45;&gt;cond</title>
<path d="M54.39,-18C61.98,-18 70.45,-18 78.74,-18"/>
<polygon points="78.72,-21.5 88.72,-18 78.72,-14.5 78.72,-21.5"/>
</g>
</g>
</svg>
</div><p>The scheduler starts with <code>init</code> task because it has no dependencies (both strong and weak dependencies). Then, the scheduler moves on to the condition task <code>cond</code>. If <code>cond</code> returns <code>0</code>, the scheduler enqueues <code>cond</code> and runs it again. If <code>cond</code> returns <code>1</code>, the scheduler enqueues <code>stop</code> and then moves on.</p></section></section><section id="AvoidCommonPitfalls"><h2><a href="#AvoidCommonPitfalls">Avoid Common Pitfalls</a></h2><p>Condition tasks are handy in creasing dynamic and cyclic control flows, but they are also easy to make mistakes. It is your responsibility to ensure a taskflow is properly conditioned. Top things to avoid include <em>no source tasks</em> to start with and <em>task race</em>. The figure below shows common pitfalls and their remedies.</p><div class="m-graph"><svg style="width: 91.000rem; height: 26.200rem;" viewBox="0.00 0.00 910.00 261.75">
<g transform="scale(1 1) rotate(0) translate(4 257.75)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_error1</title>
<polygon points="8,-91.25 8,-245.75 150,-245.75 150,-91.25 8,-91.25"/>
<text text-anchor="middle" x="79" y="-232.25" font-family="Helvetica,sans-Serif" font-size="10.00">error1: no source tasks</text>
</g>
<g class="m-cluster">
<title>cluster_fix1</title>
<polygon points="158,-8 158,-245.75 300,-245.75 300,-8 158,-8"/>
<text text-anchor="middle" x="229" y="-232.25" font-family="Helvetica,sans-Serif" font-size="10.00">fix1: adds a source</text>
</g>
<g class="m-cluster">
<title>cluster_error2</title>
<polygon points="308,-91.25 308,-245.75 450,-245.75 450,-91.25 308,-91.25"/>
<text text-anchor="middle" x="379" y="-232.25" font-family="Helvetica,sans-Serif" font-size="10.00">error2: race on D</text>
</g>
<g class="m-cluster">
<title>cluster_fix2</title>
<polygon points="458,-8 458,-245.75 672,-245.75 672,-8 458,-8"/>
<text text-anchor="middle" x="565" y="-232.25" font-family="Helvetica,sans-Serif" font-size="10.00">fix2: adds an auxiliary task</text>
</g>
<g class="m-cluster">
<title>cluster_risky</title>
<polygon points="680,-91.25 680,-245.75 894,-245.75 894,-91.25 680,-91.25"/>
<text text-anchor="middle" x="787" y="-232.25" font-family="Helvetica,sans-Serif" font-size="10.00">risky: X might be raced</text>
</g>
<g class="m-node">
<title>e1A</title>
<polygon points="57,-218.5 30,-200.5 57,-182.5 84,-200.5 57,-218.5"/>
<text text-anchor="middle" x="57" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">A</text>
</g>
<g class="m-edge">
<title>e1A&#45;&gt;e1A</title>
<path stroke-dasharray="5,2" d="M74.55,-207.3C88.14,-209.84 102,-207.57 102,-200.5 102,-195.42 94.84,-192.82 85.77,-192.7"/>
<polygon points="85.71,-189.19 76.06,-193.57 86.33,-196.16 85.71,-189.19"/>
<text text-anchor="middle" x="104.62" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>e1B</title>
<ellipse cx="43" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">B</text>
</g>
<g class="m-edge">
<title>e1A&#45;&gt;e1B</title>
<path stroke-dasharray="5,2" d="M54.3,-183.83C52.49,-173.35 50.06,-159.25 47.92,-146.8"/>
<polygon points="51.39,-146.31 46.24,-137.05 44.49,-147.5 51.39,-146.31"/>
<text text-anchor="middle" x="52.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>e1C</title>
<ellipse cx="115" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-edge">
<title>e1A&#45;&gt;e1C</title>
<path stroke-dasharray="5,2" d="M65.31,-187.86C73.58,-176.27 86.55,-158.11 97.16,-143.25"/>
<polygon points="99.88,-145.46 102.84,-135.29 94.18,-141.39 99.88,-145.46"/>
<text text-anchor="middle" x="91.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>f1S</title>
<ellipse cx="207" cy="-200.5" rx="27" ry="18"/>
<text text-anchor="middle" x="207" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">S</text>
</g>
<g class="m-node">
<title>f1A</title>
<polygon points="207,-135.25 180,-117.25 207,-99.25 234,-117.25 207,-135.25"/>
<text text-anchor="middle" x="207" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">A</text>
</g>
<g class="m-edge">
<title>f1S&#45;&gt;f1A</title>
<path d="M207,-182.27C207,-172 207,-158.7 207,-146.89"/>
<polygon points="210.5,-147.25 207,-137.25 203.5,-147.25 210.5,-147.25"/>
</g>
<g class="m-edge">
<title>f1A&#45;&gt;f1A</title>
<path stroke-dasharray="5,2" d="M224.55,-124.05C238.14,-126.59 252,-124.32 252,-117.25 252,-112.17 244.84,-109.57 235.77,-109.45"/>
<polygon points="235.71,-105.94 226.06,-110.32 236.33,-112.91 235.71,-105.94"/>
<text text-anchor="middle" x="254.62" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>f1B</title>
<ellipse cx="193" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="193" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">B</text>
</g>
<g class="m-edge">
<title>f1A&#45;&gt;f1B</title>
<path stroke-dasharray="5,2" d="M204.3,-100.58C202.49,-90.1 200.06,-76 197.92,-63.55"/>
<polygon points="201.39,-63.06 196.24,-53.8 194.49,-64.25 201.39,-63.06"/>
<text text-anchor="middle" x="202.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>f1C</title>
<ellipse cx="265" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="265" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-edge">
<title>f1A&#45;&gt;f1C</title>
<path stroke-dasharray="5,2" d="M215.31,-104.61C223.58,-93.02 236.55,-74.86 247.16,-60"/>
<polygon points="249.88,-62.21 252.84,-52.04 244.18,-58.14 249.88,-62.21"/>
<text text-anchor="middle" x="241.62" y="-71.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node">
<title>e2C</title>
<polygon points="415,-218.5 388,-200.5 415,-182.5 442,-200.5 415,-218.5"/>
<text text-anchor="middle" x="415" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-node m-flat">
<title>e2D</title>
<ellipse cx="343" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="343" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">D</text>
</g>
<g class="m-edge">
<title>e2C&#45;&gt;e2D</title>
<path stroke-dasharray="5,2" d="M405.29,-188.54C394.7,-176.59 377.44,-157.11 363.77,-141.69"/>
<polygon points="366.47,-139.46 357.22,-134.3 361.23,-144.1 366.47,-139.46"/>
<text text-anchor="middle" x="385.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>e2F</title>
<ellipse cx="415" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="415" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">F</text>
</g>
<g class="m-edge">
<title>e2C&#45;&gt;e2F</title>
<path stroke-dasharray="5,2" d="M415,-182.27C415,-172 415,-158.7 415,-146.89"/>
<polygon points="418.5,-147.25 415,-137.25 411.5,-147.25 418.5,-147.25"/>
<text text-anchor="middle" x="417.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>e2E</title>
<ellipse cx="343" cy="-200.5" rx="27" ry="18"/>
<text text-anchor="middle" x="343" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">E</text>
</g>
<g class="m-edge">
<title>e2E&#45;&gt;e2D</title>
<path d="M343,-182.27C343,-172 343,-158.7 343,-146.89"/>
<polygon points="346.5,-147.25 343,-137.25 339.5,-147.25 346.5,-147.25"/>
</g>
<g class="m-node">
<title>f2C</title>
<polygon points="590,-218.5 563,-200.5 590,-182.5 617,-200.5 590,-218.5"/>
<text text-anchor="middle" x="590" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-node m-flat">
<title>f2Daux</title>
<ellipse cx="565" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="565" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">D&#45;aux</text>
</g>
<g class="m-edge">
<title>f2C&#45;&gt;f2Daux</title>
<path stroke-dasharray="5,2" d="M585.53,-184.97C582.21,-174.17 577.57,-159.1 573.54,-146.01"/>
<polygon points="576.94,-145.15 570.65,-136.63 570.25,-147.21 576.94,-145.15"/>
<text text-anchor="middle" x="580.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>f2F</title>
<ellipse cx="637" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="637" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">F</text>
</g>
<g class="m-edge">
<title>f2C&#45;&gt;f2F</title>
<path stroke-dasharray="5,2" d="M597.34,-186.8C603.91,-175.46 613.73,-158.47 621.96,-144.25"/>
<polygon points="624.87,-146.22 626.84,-135.81 618.81,-142.71 624.87,-146.22"/>
<text text-anchor="middle" x="618.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>f2D</title>
<ellipse cx="529" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="529" y="-30.12" font-family="Helvetica,sans-Serif" font-size="10.00">D</text>
</g>
<g class="m-edge">
<title>f2Daux&#45;&gt;f2D</title>
<path d="M557.71,-99.81C552.92,-88.98 546.52,-74.54 540.98,-62.04"/>
<polygon points="544.26,-60.81 537.01,-53.08 537.86,-63.64 544.26,-60.81"/>
</g>
<g class="m-node m-flat">
<title>f2E</title>
<ellipse cx="493" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="493" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">E</text>
</g>
<g class="m-edge">
<title>f2E&#45;&gt;f2D</title>
<path d="M500.29,-99.81C505.08,-88.98 511.48,-74.54 517.02,-62.04"/>
<polygon points="520.14,-63.64 520.99,-53.08 513.74,-60.81 520.14,-63.64"/>
</g>
<g class="m-node">
<title>rP</title>
<polygon points="808,-218.5 781,-200.5 808,-182.5 835,-200.5 808,-218.5"/>
<text text-anchor="middle" x="808" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">P</text>
</g>
<g class="m-node m-flat">
<title>rQ</title>
<ellipse cx="859" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="859" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">Q</text>
</g>
<g class="m-edge">
<title>rP&#45;&gt;rQ</title>
<path stroke-dasharray="5,2" d="M815.75,-187.16C822.95,-175.69 833.89,-158.25 842.97,-143.78"/>
<polygon points="845.74,-145.96 848.09,-135.63 839.81,-142.24 845.74,-145.96"/>
<text text-anchor="middle" x="838.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>rX</title>
<ellipse cx="787" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="787" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">X</text>
</g>
<g class="m-edge">
<title>rP&#45;&gt;rX</title>
<path stroke-dasharray="5,2" d="M804.15,-184.6C801.39,-173.93 797.6,-159.26 794.28,-146.41"/>
<polygon points="797.68,-145.58 791.79,-136.77 790.9,-147.33 797.68,-145.58"/>
<text text-anchor="middle" x="800.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>rM</title>
<polygon points="729,-218.5 702,-200.5 729,-182.5 756,-200.5 729,-218.5"/>
<text text-anchor="middle" x="729" y="-196.62" font-family="Helvetica,sans-Serif" font-size="10.00">M</text>
</g>
<g class="m-edge">
<title>rM&#45;&gt;rX</title>
<path stroke-dasharray="5,2" d="M737.31,-187.86C745.58,-176.27 758.55,-158.11 769.16,-143.25"/>
<polygon points="771.88,-145.46 774.84,-135.29 766.18,-141.39 771.88,-145.46"/>
<text text-anchor="middle" x="763.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>rN</title>
<ellipse cx="715" cy="-117.25" rx="27" ry="18"/>
<text text-anchor="middle" x="715" y="-113.38" font-family="Helvetica,sans-Serif" font-size="10.00">N</text>
</g>
<g class="m-edge">
<title>rM&#45;&gt;rN</title>
<path stroke-dasharray="5,2" d="M726.3,-183.83C724.49,-173.35 722.06,-159.25 719.92,-146.8"/>
<polygon points="723.39,-146.31 718.24,-137.05 716.49,-147.5 723.39,-146.31"/>
<text text-anchor="middle" x="724.62" y="-155" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
</g>
</svg>
</div><p>In the <code>error1</code> scenario, there is no source task for the scheduler to start with, and the simplest fix is to add a task <code>S</code> that has no dependents. In the <code>error2</code> scenario, <code>D</code> might be scheduled twice by <code>E</code> through the strong dependency and <code>C</code> through the weak dependency (on returning <code>1</code>). To fix this problem, you can add an auxiliary task <code>D-aux</code> to break the mixed use of strong dependency and weak dependency. In the risky scenario, task <code>X</code> may be raced by <code>M</code> and <code>P</code> if <code>M</code> returns <code>0</code> and P returns <code>1</code>.</p><aside class="m-note m-warning"><h4>Attention</h4><p>It is your responsibility to ensure a written taskflow graph is properly conditioned. We suggest that you <a href="ConditionalTasking.html#TaskSchedulingPolicy" class="m-doc">Understand our Task-level Scheduling</a> and infer if task race exists in the execution of your graph.</p></aside></section><section id="ImplementControlFlowGraphs"><h2><a href="#ImplementControlFlowGraphs">Implement Control-flow Graphs</a></h2><section id="ImplementIfElseControlFlow"><h3><a href="#ImplementIfElseControlFlow">Implement If-Else Control Flow</a></h3><p>You can use conditional tasking to implement if-else control flow. The following example creates a nested if-else control flow diagram that executes three condition tasks to check the range of <code>i</code>.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>

<span class="c1">// create three condition tasks for nested control flow</span>
<span class="k">auto</span><span class="w"> </span><span class="n">initi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">cond1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="o">&gt;</span><span class="mi">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">cond2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="o">&gt;</span><span class="mi">2</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">cond3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="o">&gt;</span><span class="mi">3</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">equl1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i=1</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">equl2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i=2</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">equl3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i=3</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>
<span class="k">auto</span><span class="w"> </span><span class="n">grtr3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i&gt;3</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"> </span>

<span class="n">initi</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond1</span><span class="p">);</span>
<span class="n">cond1</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">equl1</span><span class="p">,</span><span class="w"> </span><span class="n">cond2</span><span class="p">);</span><span class="w">  </span><span class="c1">// goes to cond2 if i&gt;1</span>
<span class="n">cond2</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">equl2</span><span class="p">,</span><span class="w"> </span><span class="n">cond3</span><span class="p">);</span><span class="w">  </span><span class="c1">// goes to cond3 if i&gt;2</span>
<span class="n">cond3</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">equl3</span><span class="p">,</span><span class="w"> </span><span class="n">grtr3</span><span class="p">);</span><span class="w">  </span><span class="c1">// goes to grtr3 if i&gt;3</span></pre><div class="m-graph"><svg style="width: 75.600rem; height: 15.200rem;" viewBox="0.00 0.00 756.23 152.00">
<g transform="scale(1 1) rotate(0) translate(4 148)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fc231700030</title>
<ellipse cx="27" cy="-99" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-95.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7fc231700140</title>
<polygon points="170.91,-117 91,-99 170.91,-81 250.83,-99 170.91,-117"/>
<text text-anchor="middle" x="170.91" y="-95.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond1 (i&gt;1 ? 1 : 0)</text>
</g>
<g class="m-edge">
<title>p0x7fc231700030&#45;&gt;p0x7fc231700140</title>
<path d="M54.36,-99C61.25,-99 69.08,-99 77.34,-99"/>
<polygon points="77.32,-102.5 87.32,-99 77.32,-95.5 77.32,-102.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7fc231700470</title>
<ellipse cx="371.99" cy="-126" rx="27" ry="18"/>
<text text-anchor="middle" x="371.99" y="-122.12" font-family="Helvetica,sans-Serif" font-size="10.00">equl1</text>
</g>
<g class="m-edge">
<title>p0x7fc231700140&#45;&gt;p0x7fc231700470</title>
<path stroke-dasharray="5,2" d="M222.41,-105.85C257.13,-110.56 302.48,-116.71 333.9,-120.97"/>
<polygon points="333.33,-124.42 343.71,-122.3 334.27,-117.49 333.33,-124.42"/>
<text text-anchor="middle" x="271.45" y="-113.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7fc231700250</title>
<polygon points="371.99,-90 292.08,-72 371.99,-54 451.9,-72 371.99,-90"/>
<text text-anchor="middle" x="371.99" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond2 (i&gt;2 ? 1 : 0)</text>
</g>
<g class="m-edge">
<title>p0x7fc231700140&#45;&gt;p0x7fc231700250</title>
<path stroke-dasharray="5,2" d="M222.41,-92.15C248.83,-88.57 281.4,-84.15 309.31,-80.37"/>
<polygon points="309.61,-83.86 319.05,-79.04 308.67,-76.92 309.61,-83.86"/>
<text text-anchor="middle" x="271.45" y="-86.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fc231700580</title>
<ellipse cx="573.06" cy="-99" rx="27" ry="18"/>
<text text-anchor="middle" x="573.06" y="-95.12" font-family="Helvetica,sans-Serif" font-size="10.00">equl2</text>
</g>
<g class="m-edge">
<title>p0x7fc231700250&#45;&gt;p0x7fc231700580</title>
<path stroke-dasharray="5,2" d="M423.48,-78.85C458.21,-83.56 503.56,-89.71 534.98,-93.97"/>
<polygon points="534.41,-97.42 544.79,-95.3 535.35,-90.49 534.41,-97.42"/>
<text text-anchor="middle" x="472.53" y="-86.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7fc231700360</title>
<polygon points="573.06,-63 493.15,-45 573.06,-27 652.98,-45 573.06,-63"/>
<text text-anchor="middle" x="573.06" y="-41.12" font-family="Helvetica,sans-Serif" font-size="10.00">cond3 (i&gt;3 ? 1 : 0)</text>
</g>
<g class="m-edge">
<title>p0x7fc231700250&#45;&gt;p0x7fc231700360</title>
<path stroke-dasharray="5,2" d="M423.48,-65.15C449.91,-61.57 482.48,-57.15 510.39,-53.37"/>
<polygon points="510.69,-56.86 520.13,-52.04 509.75,-49.92 510.69,-56.86"/>
<text text-anchor="middle" x="472.53" y="-59.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fc231700690</title>
<ellipse cx="721.23" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="721.23" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">equl3</text>
</g>
<g class="m-edge">
<title>p0x7fc231700360&#45;&gt;p0x7fc231700690</title>
<path stroke-dasharray="5,2" d="M618.32,-53.17C639.25,-57.04 663.93,-61.6 683.77,-65.27"/>
<polygon points="682.86,-68.66 693.33,-67.03 684.14,-61.77 682.86,-68.66"/>
<text text-anchor="middle" x="673.6" y="-64.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7fc2317007a0</title>
<ellipse cx="721.23" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="721.23" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">grtr3</text>
</g>
<g class="m-edge">
<title>p0x7fc231700360&#45;&gt;p0x7fc2317007a0</title>
<path stroke-dasharray="5,2" d="M618.32,-36.83C639.25,-32.96 663.93,-28.4 683.77,-24.73"/>
<polygon points="684.14,-28.23 693.33,-22.97 682.86,-21.34 684.14,-28.23"/>
<text text-anchor="middle" x="673.6" y="-27.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
</g>
</svg>
</div></section><section id="ImplementSwitchControlFlow"><h3><a href="#ImplementSwitchControlFlow">Implement Switch Control Flow</a></h3><p>You can use conditional tasking to implement <em>switch</em> control flow. The following example creates a switch control flow diagram that executes one of the three cases at random using four condition tasks.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">source</span><span class="p">,</span><span class="w"> </span><span class="n">swcond</span><span class="p">,</span><span class="w"> </span><span class="n">case1</span><span class="p">,</span><span class="w"> </span><span class="n">case2</span><span class="p">,</span><span class="w"> </span><span class="n">case3</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;source</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;switch</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;case 1</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;case 2</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;case 3</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;target</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">);</span>

<span class="n">source</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">swcond</span><span class="p">);</span>
<span class="n">swcond</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">case1</span><span class="p">,</span><span class="w"> </span><span class="n">case2</span><span class="p">,</span><span class="w"> </span><span class="n">case3</span><span class="p">);</span>
<span class="n">target</span><span class="p">.</span><span class="n">succeed</span><span class="p">(</span><span class="n">case1</span><span class="p">,</span><span class="w"> </span><span class="n">case2</span><span class="p">,</span><span class="w"> </span><span class="n">case3</span><span class="p">);</span></pre><div class="m-graph"><svg style="width: 38.600rem; height: 15.200rem;" viewBox="0.00 0.00 385.70 152.00">
<g transform="scale(1 1) rotate(0) translate(4 148)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fa25f000030</title>
<ellipse cx="27" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">source</text>
</g>
<g class="m-node">
<title>p0x7fa25f000140</title>
<polygon points="128.24,-90 91,-72 128.24,-54 165.47,-72 128.24,-90"/>
<text text-anchor="middle" x="128.24" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">switch</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000030&#45;&gt;p0x7fa25f000140</title>
<path d="M54.33,-72C61.83,-72 70.25,-72 78.63,-72"/>
<polygon points="78.43,-75.5 88.43,-72 78.43,-68.5 78.43,-75.5"/>
</g>
<g class="m-node">
<title>p0x7fa25f000250</title>
<polygon points="244.59,-144 206.72,-126 244.59,-108 282.45,-126 244.59,-144"/>
<text text-anchor="middle" x="244.59" y="-122.12" font-family="Helvetica,sans-Serif" font-size="10.00">case 1</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000140&#45;&gt;p0x7fa25f000250</title>
<path stroke-dasharray="5,2" d="M148.12,-80.92C166.16,-89.44 193.62,-102.4 214.46,-112.25"/>
<polygon points="212.77,-115.32 223.31,-116.42 215.76,-108.99 212.77,-115.32"/>
<text text-anchor="middle" x="186.1" y="-100.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7fa25f000360</title>
<polygon points="244.59,-90 206.72,-72 244.59,-54 282.45,-72 244.59,-90"/>
<text text-anchor="middle" x="244.59" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">case 2</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000140&#45;&gt;p0x7fa25f000360</title>
<path stroke-dasharray="5,2" d="M166.36,-72C175.36,-72 185.11,-72 194.56,-72"/>
<polygon points="194.35,-75.5 204.35,-72 194.35,-68.5 194.35,-75.5"/>
<text text-anchor="middle" x="186.1" y="-73.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>p0x7fa25f000470</title>
<polygon points="244.59,-36 206.72,-18 244.59,0 282.45,-18 244.59,-36"/>
<text text-anchor="middle" x="244.59" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">case 3</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000140&#45;&gt;p0x7fa25f000470</title>
<path stroke-dasharray="5,2" d="M148.12,-63.08C166.16,-54.56 193.62,-41.6 214.46,-31.75"/>
<polygon points="215.76,-35.01 223.31,-27.58 212.77,-28.68 215.76,-35.01"/>
<text text-anchor="middle" x="186.1" y="-46.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x7fa25f000580</title>
<ellipse cx="350.7" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="350.7" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">target</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000250&#45;&gt;p0x7fa25f000580</title>
<path stroke-dasharray="5,2" d="M263.72,-116.59C278.92,-108.71 300.94,-97.29 318.9,-87.98"/>
<polygon points="320.31,-91.19 327.57,-83.48 317.08,-84.98 320.31,-91.19"/>
<text text-anchor="middle" x="303.08" y="-98.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000360&#45;&gt;p0x7fa25f000580</title>
<path stroke-dasharray="5,2" d="M283.47,-72C292.79,-72 302.77,-72 312.08,-72"/>
<polygon points="311.9,-75.5 321.9,-72 311.9,-68.5 311.9,-75.5"/>
<text text-anchor="middle" x="303.08" y="-73.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fa25f000470&#45;&gt;p0x7fa25f000580</title>
<path stroke-dasharray="5,2" d="M263.72,-27.41C278.92,-35.29 300.94,-46.71 318.9,-56.02"/>
<polygon points="317.08,-59.02 327.57,-60.52 320.31,-52.81 317.08,-59.02"/>
<text text-anchor="middle" x="303.08" y="-49.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
</g>
</svg>
</div><p>Assuming <code>swcond</code> returns 1, the program outputs:</p><pre class="m-console"><span class="go">source</span>
<span class="go">switch</span>
<span class="go">case 2</span>
<span class="go">target</span></pre><p>Keep in mind, both switch and case tasks must be described as condition tasks. The following implementation is a common mistake in which case tasks are not described as condition tasks.</p><pre class="m-code"><span class="c1">// wrong implementation of switch control flow using only one condition task</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">source</span><span class="p">,</span><span class="w"> </span><span class="n">swcond</span><span class="p">,</span><span class="w"> </span><span class="n">case1</span><span class="p">,</span><span class="w"> </span><span class="n">case2</span><span class="p">,</span><span class="w"> </span><span class="n">case3</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;source</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;switch</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;case 1</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;case 2</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;case 3</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;target</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w">  </span><span class="c1">// target has three strong dependencies</span>
<span class="p">);</span>

<span class="n">source</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">swcond</span><span class="p">);</span>
<span class="n">swcond</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">case1</span><span class="p">,</span><span class="w"> </span><span class="n">case2</span><span class="p">,</span><span class="w"> </span><span class="n">case3</span><span class="p">);</span>
<span class="n">target</span><span class="p">.</span><span class="n">succeed</span><span class="p">(</span><span class="n">case1</span><span class="p">,</span><span class="w"> </span><span class="n">case2</span><span class="p">,</span><span class="w"> </span><span class="n">case3</span><span class="p">);</span></pre><div class="m-graph"><svg style="width: 36.000rem; height: 15.200rem;" viewBox="0.00 0.00 359.72 152.00">
<g transform="scale(1 1) rotate(0) translate(4 148)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7f96d3000030</title>
<ellipse cx="27" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">source</text>
</g>
<g class="m-node">
<title>p0x7f96d3000140</title>
<polygon points="128.24,-90 91,-72 128.24,-54 165.47,-72 128.24,-90"/>
<text text-anchor="middle" x="128.24" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">switch</text>
</g>
<g class="m-edge">
<title>p0x7f96d3000030&#45;&gt;p0x7f96d3000140</title>
<path d="M54.33,-72C61.83,-72 70.25,-72 78.63,-72"/>
<polygon points="78.43,-75.5 88.43,-72 78.43,-68.5 78.43,-75.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7f96d3000250</title>
<ellipse cx="233.72" cy="-126" rx="27" ry="18"/>
<text text-anchor="middle" x="233.72" y="-122.12" font-family="Helvetica,sans-Serif" font-size="10.00">case 1</text>
</g>
<g class="m-edge">
<title>p0x7f96d3000140&#45;&gt;p0x7f96d3000250</title>
<path stroke-dasharray="5,2" d="M146.79,-81.16C161.83,-89.01 183.85,-100.5 201.83,-109.88"/>
<polygon points="200.04,-112.89 210.53,-114.42 203.28,-106.69 200.04,-112.89"/>
<text text-anchor="middle" x="186.1" y="-103.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7f96d3000360</title>
<ellipse cx="233.72" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="233.72" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">case 2</text>
</g>
<g class="m-edge">
<title>p0x7f96d3000140&#45;&gt;p0x7f96d3000360</title>
<path stroke-dasharray="5,2" d="M166.6,-72C175.88,-72 185.83,-72 195.12,-72"/>
<polygon points="194.93,-75.5 204.93,-72 194.93,-68.5 194.93,-75.5"/>
<text text-anchor="middle" x="186.1" y="-73.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f96d3000470</title>
<ellipse cx="233.72" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="233.72" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">case 3</text>
</g>
<g class="m-edge">
<title>p0x7f96d3000140&#45;&gt;p0x7f96d3000470</title>
<path stroke-dasharray="5,2" d="M146.79,-62.84C161.83,-54.99 183.85,-43.5 201.83,-34.12"/>
<polygon points="203.28,-37.31 210.53,-29.58 200.04,-31.11 203.28,-37.31"/>
<text text-anchor="middle" x="186.1" y="-44.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x7f96d3000580</title>
<ellipse cx="324.72" cy="-72" rx="27" ry="18"/>
<text text-anchor="middle" x="324.72" y="-68.12" font-family="Helvetica,sans-Serif" font-size="10.00">target</text>
</g>
<g class="m-edge">
<title>p0x7f96d3000250&#45;&gt;p0x7f96d3000580</title>
<path d="M254.48,-114.02C266.23,-106.88 281.36,-97.7 294.48,-89.75"/>
<polygon points="296,-92.92 302.73,-84.74 292.37,-86.93 296,-92.92"/>
</g>
<g class="m-edge">
<title>p0x7f96d3000360&#45;&gt;p0x7f96d3000580</title>
<path d="M260.95,-72C268.84,-72 277.67,-72 286.15,-72"/>
<polygon points="285.87,-75.5 295.87,-72 285.87,-68.5 285.87,-75.5"/>
</g>
<g class="m-edge">
<title>p0x7f96d3000470&#45;&gt;p0x7f96d3000580</title>
<path d="M254.48,-29.98C266.23,-37.12 281.36,-46.3 294.48,-54.25"/>
<polygon points="292.37,-57.07 302.73,-59.26 296,-51.08 292.37,-57.07"/>
</g>
</g>
</svg>
</div><p>In this faulty implementation, task <code>target</code> has three strong dependencies but only one of them will be met. This is because <code>swcond</code> is a condition task, and only one case task will be executed depending on the return of <code>swcond</code>.</p></section><section id="ImplementDoWhileLoopControlFlow"><h3><a href="#ImplementDoWhileLoopControlFlow">Implement Do-While-Loop Control Flow</a></h3><p>You can use conditional tasking to implement <em>do-while-loop</em> control flow. The following example creates a do-while-loop control flow diagram that repeatedly increments variable <code>i</code> five times using one condition task.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>

<span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">init</span><span class="p">,</span><span class="w"> </span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">cond</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i=0</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i++ =&gt; i=&quot;</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="sc">&#39;\n&#39;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="o">&lt;</span><span class="mi">5</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;done</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">);</span><span class="w">  </span>

<span class="n">init</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">body</span><span class="p">);</span>
<span class="n">body</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">);</span>
<span class="n">cond</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">);</span></pre><div class="m-graph"><svg style="width: 38.200rem; height: 4.400rem;" viewBox="0.00 0.00 381.55 44.00">
<g transform="scale(1 1) rotate(0) translate(4 40)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7f950d600030</title>
<ellipse cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node m-flat">
<title>p0x7f950d600140</title>
<ellipse cx="118" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="118" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">do i++</text>
</g>
<g class="m-edge">
<title>p0x7f950d600030&#45;&gt;p0x7f950d600140</title>
<path d="M54.22,-18C62.12,-18 70.94,-18 79.43,-18"/>
<polygon points="79.14,-21.5 89.14,-18 79.14,-14.5 79.14,-21.5"/>
</g>
<g class="m-node">
<title>p0x7f950d600250</title>
<polygon points="232.27,-36 186.25,-18 232.27,0 278.3,-18 232.27,-36"/>
<text text-anchor="middle" x="232.27" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">while i&lt;5</text>
</g>
<g class="m-edge">
<title>p0x7f950d600140&#45;&gt;p0x7f950d600250</title>
<path d="M145.32,-18C153.8,-18 163.56,-18 173.39,-18"/>
<polygon points="173.37,-21.5 183.37,-18 173.37,-14.5 173.37,-21.5"/>
</g>
<g class="m-edge">
<title>p0x7f950d600250&#45;&gt;p0x7f950d600140</title>
<path stroke-dasharray="5,2" d="M208.31,-8.96C195.11,-4.84 178.19,-1.35 163,-3.75 159.62,-4.28 156.14,-5.03 152.7,-5.92"/>
<polygon points="152.02,-2.47 143.41,-8.64 153.99,-9.18 152.02,-2.47"/>
<text text-anchor="middle" x="165.62" y="-5.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7f950d600360</title>
<ellipse cx="346.55" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="346.55" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">done</text>
</g>
<g class="m-edge">
<title>p0x7f950d600250&#45;&gt;p0x7f950d600360</title>
<path stroke-dasharray="5,2" d="M279.59,-18C289,-18 298.78,-18 307.85,-18"/>
<polygon points="307.75,-21.5 317.75,-18 307.75,-14.5 307.75,-21.5"/>
<text text-anchor="middle" x="298.92" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
</g>
</svg>
</div><p>The program outputs:</p><pre class="m-console"><span class="go">i=0</span>
<span class="go">i++ =&gt; i=1</span>
<span class="go">i++ =&gt; i=2</span>
<span class="go">i++ =&gt; i=3</span>
<span class="go">i++ =&gt; i=4</span>
<span class="go">i++ =&gt; i=5</span>
<span class="go">done</span></pre></section><section id="ImplementWhileLoopControlFlow"><h3><a href="#ImplementWhileLoopControlFlow">Implement While-Loop Control Flow</a></h3><p>You can use conditional tasking to implement <em>while-loop</em> control flow. The following example creates a while-loop control flow diagram that repeatedly increments variable <code>i</code> five times using two condition task.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>

<span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">init</span><span class="p">,</span><span class="w"> </span><span class="n">cond</span><span class="p">,</span><span class="w"> </span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">back</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i=0</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;while i&lt;5</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i++=&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="sc">&#39;\n&#39;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;back</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;done</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">);</span>

<span class="n">init</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">);</span>
<span class="n">cond</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">);</span>
<span class="n">body</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">back</span><span class="p">);</span>
<span class="n">back</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">);</span></pre><div class="m-graph"><svg style="width: 38.500rem; height: 13.300rem;" viewBox="0.00 0.00 385.22 133.00">
<g transform="scale(1 1) rotate(0) translate(4 129)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fdba4000030</title>
<ellipse cx="27" cy="-53" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-49.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7fdba4000140</title>
<polygon points="137.02,-71 91,-53 137.02,-35 183.05,-53 137.02,-71"/>
<text text-anchor="middle" x="137.02" y="-49.12" font-family="Helvetica,sans-Serif" font-size="10.00">while i&lt;5</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000030&#45;&gt;p0x7fdba4000140</title>
<path d="M54.43,-53C61.73,-53 69.94,-53 78.28,-53"/>
<polygon points="78.14,-56.5 88.14,-53 78.14,-49.5 78.14,-56.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7fdba4000470</title>
<ellipse cx="251.3" cy="-107" rx="27" ry="18"/>
<text text-anchor="middle" x="251.3" y="-103.12" font-family="Helvetica,sans-Serif" font-size="10.00">done</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000140&#45;&gt;p0x7fdba4000470</title>
<path stroke-dasharray="5,2" d="M158.62,-62.9C175.39,-70.97 199.35,-82.5 218.57,-91.74"/>
<polygon points="216.79,-94.77 227.32,-95.95 219.83,-88.46 216.79,-94.77"/>
<text text-anchor="middle" x="203.67" y="-85.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fdba4000250</title>
<ellipse cx="251.3" cy="-53" rx="27" ry="18"/>
<text text-anchor="middle" x="251.3" y="-49.12" font-family="Helvetica,sans-Serif" font-size="10.00">i++</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000140&#45;&gt;p0x7fdba4000250</title>
<path stroke-dasharray="5,2" d="M184.34,-53C193.75,-53 203.53,-53 212.6,-53"/>
<polygon points="212.5,-56.5 222.5,-53 212.5,-49.5 212.5,-56.5"/>
<text text-anchor="middle" x="203.67" y="-54.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7fdba4000360</title>
<polygon points="346.26,-36 315.3,-18 346.26,0 377.22,-18 346.26,-36"/>
<text text-anchor="middle" x="346.26" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">back</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000250&#45;&gt;p0x7fdba4000360</title>
<path d="M275.28,-44.35C287.62,-39.7 302.98,-33.92 316.15,-28.96"/>
<polygon points="317.06,-32.36 325.18,-25.56 314.59,-25.81 317.06,-32.36"/>
</g>
<g class="m-edge">
<title>p0x7fdba4000360&#45;&gt;p0x7fdba4000140</title>
<path stroke-dasharray="5,2" d="M321.32,-14.09C296.7,-10.84 257.39,-7.74 224.3,-14.75 204.05,-19.04 182.77,-28.41 166.42,-36.8"/>
<polygon points="164.96,-33.61 157.78,-41.4 168.25,-39.79 164.96,-33.61"/>
<text text-anchor="middle" x="251.3" y="-15.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
</g>
</svg>
</div><p>The program outputs:</p><pre class="m-console"><span class="go">i=0</span>
<span class="go">while i&lt;5</span>
<span class="go">i++=0</span>
<span class="go">back</span>
<span class="go">while i&lt;5</span>
<span class="go">i++=1</span>
<span class="go">back</span>
<span class="go">while i&lt;5</span>
<span class="go">i++=2</span>
<span class="go">back</span>
<span class="go">while i&lt;5</span>
<span class="go">i++=3</span>
<span class="go">back</span>
<span class="go">while i&lt;5</span>
<span class="go">i++=4</span>
<span class="go">back</span>
<span class="go">while i&lt;5</span>
<span class="go">done</span></pre><p>Notice that, when you implement a while-loop block, you cannot direct a dependency from the body task to the loop condition task. Doing so will introduce a strong dependency between the body task and the loop condition task, and the loop condition task will never be executed. The following code shows a common faulty implementation of while-loop control flow.</p><pre class="m-code"><span class="c1">// wrong implementation of while-loop using only one condition task</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>

<span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">init</span><span class="p">,</span><span class="w"> </span><span class="n">cond</span><span class="p">,</span><span class="w"> </span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i=0</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;while i&lt;5</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;i++=&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="sc">&#39;\n&#39;</span><span class="p">;</span><span class="w"> </span><span class="p">},</span>
<span class="w">  </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;done</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="p">);</span>

<span class="n">init</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">);</span>
<span class="n">cond</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">done</span><span class="p">);</span>
<span class="n">body</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">cond</span><span class="p">);</span></pre><div class="m-graph"><svg style="width: 28.600rem; height: 10.400rem;" viewBox="0.00 0.00 286.30 104.00">
<g transform="scale(1 1) rotate(0) translate(4 100)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fdba4000030</title>
<ellipse cx="27" cy="-44" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-40.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7fdba4000140</title>
<polygon points="137.02,-62 91,-44 137.02,-26 183.05,-44 137.02,-62"/>
<text text-anchor="middle" x="137.02" y="-40.12" font-family="Helvetica,sans-Serif" font-size="10.00">while i&lt;5</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000030&#45;&gt;p0x7fdba4000140</title>
<path d="M54.43,-44C61.73,-44 69.94,-44 78.28,-44"/>
<polygon points="78.14,-47.5 88.14,-44 78.14,-40.5 78.14,-47.5"/>
</g>
<g class="m-node m-flat">
<title>p0x7fdba4000470</title>
<ellipse cx="251.3" cy="-78" rx="27" ry="18"/>
<text text-anchor="middle" x="251.3" y="-74.12" font-family="Helvetica,sans-Serif" font-size="10.00">done</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000140&#45;&gt;p0x7fdba4000470</title>
<path stroke-dasharray="5,2" d="M164.06,-51.89C179.33,-56.51 198.8,-62.41 215.39,-67.43"/>
<polygon points="214.28,-70.75 224.87,-70.3 216.31,-64.05 214.28,-70.75"/>
<text text-anchor="middle" x="203.67" y="-65.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fdba4000250</title>
<ellipse cx="251.3" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="251.3" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">i++</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000140&#45;&gt;p0x7fdba4000250</title>
<path stroke-dasharray="5,2" d="M166.94,-37.3C181.33,-33.97 198.87,-29.91 214.15,-26.37"/>
<polygon points="214.87,-29.8 223.82,-24.13 213.29,-22.98 214.87,-29.8"/>
<text text-anchor="middle" x="203.67" y="-29.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fdba4000250&#45;&gt;p0x7fdba4000140</title>
<path d="M224.35,-20.94C216.79,-21.99 208.55,-23.35 201.05,-25 192.02,-26.99 182.41,-29.62 173.48,-32.28"/>
<polygon points="172.61,-28.89 164.09,-35.19 174.67,-35.58 172.61,-28.89"/>
</g>
</g>
</svg>
</div><p>In the taskflow diagram above, the scheduler starts with <code>init</code> and then decrements the strong dependency of the loop condition task, <code>while i&lt;5</code>. After this, there remains one strong dependency, i.e., introduced by the loop body task, <code>i++</code>. However, task <code>i++</code> will not be executed until the loop condition task returns <code>0</code>, causing a deadlock.</p></section></section><section id="CreateAMultiConditionTask"><h2><a href="#CreateAMultiConditionTask">Create a Multi-condition Task</a></h2><p>A <em>multi-condition task</em> is a generalized version of conditional tasking. In some cases, applications need to jump to multiple branches from a parent task. This can be done by creating a <em>multi-condition task</em> which allows a task to select one or more successor tasks to execute. Similar to a condition task, a multi-condition task returns a vector of integer indices that indicate the successors to execute when the multi-condition task completes. The index is defined with respect to the order of successors preceded by a multi-condition task. For example, the following code creates a multi-condition task, <code>A</code>, that informs the scheduler to run on its two successors, <code>B</code> and <code>D</code>.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>

<span class="k">auto</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">]()</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">SmallVector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w">  </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;A</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">};</span>
<span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;A&quot;</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;B</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;B&quot;</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;C</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;C&quot;</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;D</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">}).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;D&quot;</span><span class="p">);</span>

<span class="n">A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="p">,</span><span class="w"> </span><span class="n">D</span><span class="p">);</span>

<span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span></pre><div class="m-graph"><svg style="width: 20.600rem; height: 12.700rem;" viewBox="0.00 0.00 206.00 127.25">
<g transform="scale(1 1) rotate(0) translate(4 123.25)">
<title>Taskflow</title>
<g class="m-node">
<title>p0x7bc400014030</title>
<polygon points="99,-119.25 72,-101.25 99,-83.25 126,-101.25 99,-119.25"/>
<text text-anchor="middle" x="99" y="-97.38" font-family="Helvetica,sans-Serif" font-size="10.00">A</text>
</g>
<g class="m-node m-flat">
<title>p0x7bc400014118</title>
<ellipse cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">B</text>
</g>
<g class="m-edge">
<title>p0x7bc400014030&#45;&gt;p0x7bc400014118</title>
<path stroke-dasharray="5,2" d="M89.29,-89.29C78.7,-77.34 61.44,-57.86 47.77,-42.44"/>
<polygon points="50.47,-40.21 41.22,-35.05 45.23,-44.85 50.47,-40.21"/>
<text text-anchor="middle" x="70.62" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node m-flat">
<title>p0x7bc400014200</title>
<ellipse cx="99" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="99" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-edge">
<title>p0x7bc400014030&#45;&gt;p0x7bc400014200</title>
<path stroke-dasharray="5,2" d="M99,-83.02C99,-72.75 99,-59.45 99,-47.64"/>
<polygon points="102.5,-48 99,-38 95.5,-48 102.5,-48"/>
<text text-anchor="middle" x="101.62" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7bc4000142e8</title>
<ellipse cx="171" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="171" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">D</text>
</g>
<g class="m-edge">
<title>p0x7bc400014030&#45;&gt;p0x7bc4000142e8</title>
<path stroke-dasharray="5,2" d="M108.71,-89.29C119.3,-77.34 136.56,-57.86 150.23,-42.44"/>
<polygon points="152.77,-44.85 156.78,-35.05 147.53,-40.21 152.77,-44.85"/>
<text text-anchor="middle" x="141.62" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
</g>
</svg>
</div><aside class="m-note m-info"><h4>Note</h4><p>The return type of a multi-condition task is <a href="classtf_1_1SmallVector.html" class="m-doc">tf::<wbr />SmallVector</a>, which provides C++ vector-style functionalities but comes with small buffer optimization.</p></aside><p>One important application of conditional tasking is implementing <em>iterative control flow</em>. You can use multi-condition tasks to create multiple loops that run concurrently. The following code creates a sequential chain of four loops in which each loop increments a counter variable ten times. When the program completes, the value of the counter variable is <code>40</code>.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span>
<span class="n">std</span><span class="o">::</span><span class="n">atomic</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">counter</span><span class="p">{</span><span class="mi">0</span><span class="p">};</span>

<span class="k">auto</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="kt">bool</span><span class="p">{</span><span class="nb">true</span><span class="p">},</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">int</span><span class="p">(</span><span class="mi">0</span><span class="p">)]()</span><span class="w"> </span><span class="k">mutable</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">SmallVector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">false</span><span class="p">;</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">-1</span><span class="p">};</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="n">counter</span><span class="p">.</span><span class="n">fetch_add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">memory_order_relaxed</span><span class="p">);</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="p">{</span><span class="o">++</span><span class="n">c</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">-1</span><span class="p">};</span>
<span class="w">  </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">auto</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){});</span>
<span class="k">auto</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span><span class="n">loop</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span><span class="n">loop</span><span class="p">);</span>
<span class="k">auto</span><span class="w"> </span><span class="n">D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span><span class="n">loop</span><span class="p">);</span>

<span class="n">A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">);</span>
<span class="n">B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="p">);</span>
<span class="n">C</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">C</span><span class="p">,</span><span class="w"> </span><span class="n">D</span><span class="p">);</span>
<span class="n">D</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">D</span><span class="p">);</span>

<span class="n">executor</span><span class="p">.</span><span class="n">run</span><span class="p">(</span><span class="n">taskflow</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span><span class="w">  </span><span class="c1">// counter == 40</span></pre><div class="m-graph"><svg style="width: 43.900rem; height: 7.300rem;" viewBox="0.00 0.00 438.75 73.25">
<g transform="scale(1 1) rotate(0) translate(4 69.25)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7bc400014030</title>
<ellipse cx="27" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7bc400014118</title>
<polygon points="118,-36 91,-18 118,0 145,-18 118,-36"/>
<text text-anchor="middle" x="118" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">A</text>
</g>
<g class="m-edge">
<title>p0x7bc400014030&#45;&gt;p0x7bc400014118</title>
<path d="M54.22,-18C61.96,-18 70.59,-18 78.91,-18"/>
<polygon points="78.84,-21.5 88.84,-18 78.84,-14.5 78.84,-21.5"/>
</g>
<g class="m-edge">
<title>p0x7bc400014118&#45;&gt;p0x7bc400014118</title>
<path stroke-dasharray="5,2" d="M110.67,-31.67C107.74,-42.66 110.18,-54 118,-54 123.01,-54 125.81,-49.34 126.41,-43.11"/>
<polygon points="129.9,-42.8 125.47,-33.17 122.93,-43.46 129.9,-42.8"/>
<text text-anchor="middle" x="118" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7bc400014200</title>
<polygon points="213.25,-36 186.25,-18 213.25,0 240.25,-18 213.25,-36"/>
<text text-anchor="middle" x="213.25" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">B</text>
</g>
<g class="m-edge">
<title>p0x7bc400014118&#45;&gt;p0x7bc400014200</title>
<path stroke-dasharray="5,2" d="M145.47,-18C154.35,-18 164.44,-18 174.04,-18"/>
<polygon points="173.87,-21.5 183.87,-18 173.87,-14.5 173.87,-21.5"/>
<text text-anchor="middle" x="165.62" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7bc400014200&#45;&gt;p0x7bc400014200</title>
<path stroke-dasharray="5,2" d="M205.68,-31.29C202.45,-42.4 204.97,-54 213.25,-54 218.68,-54 221.64,-49 222.11,-42.43"/>
<polygon points="225.62,-42.32 220.99,-32.8 218.67,-43.13 225.62,-42.32"/>
<text text-anchor="middle" x="213.25" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7bc4000142e8</title>
<polygon points="308.5,-36 281.5,-18 308.5,0 335.5,-18 308.5,-36"/>
<text text-anchor="middle" x="308.5" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">C</text>
</g>
<g class="m-edge">
<title>p0x7bc400014200&#45;&gt;p0x7bc4000142e8</title>
<path stroke-dasharray="5,2" d="M240.72,-18C249.6,-18 259.69,-18 269.29,-18"/>
<polygon points="269.12,-21.5 279.12,-18 269.12,-14.5 269.12,-21.5"/>
<text text-anchor="middle" x="260.88" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7bc4000142e8&#45;&gt;p0x7bc4000142e8</title>
<path stroke-dasharray="5,2" d="M300.93,-31.29C297.7,-42.4 300.22,-54 308.5,-54 313.93,-54 316.89,-49 317.36,-42.43"/>
<polygon points="320.87,-42.32 316.24,-32.8 313.92,-43.13 320.87,-42.32"/>
<text text-anchor="middle" x="308.5" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-node">
<title>p0x7bc4000143d0</title>
<polygon points="403.75,-36 376.75,-18 403.75,0 430.75,-18 403.75,-36"/>
<text text-anchor="middle" x="403.75" y="-14.12" font-family="Helvetica,sans-Serif" font-size="10.00">D</text>
</g>
<g class="m-edge">
<title>p0x7bc4000142e8&#45;&gt;p0x7bc4000143d0</title>
<path stroke-dasharray="5,2" d="M335.97,-18C344.85,-18 354.94,-18 364.54,-18"/>
<polygon points="364.37,-21.5 374.37,-18 364.37,-14.5 364.37,-21.5"/>
<text text-anchor="middle" x="356.12" y="-19.75" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7bc4000143d0&#45;&gt;p0x7bc4000143d0</title>
<path stroke-dasharray="5,2" d="M396.18,-31.29C392.95,-42.4 395.47,-54 403.75,-54 409.18,-54 412.14,-49 412.61,-42.43"/>
<polygon points="416.12,-42.32 411.49,-32.8 409.17,-43.13 416.12,-42.32"/>
<text text-anchor="middle" x="403.75" y="-55.75" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
</g>
</svg>
</div><aside class="m-note m-warning"><h4>Attention</h4><p>It is your responsibility to ensure the return of a multi-condition task goes to a correct successor task. If a returned index falls outside the successor range of a multi-condition task, the scheduler will skip that index without doing anything.</p></aside></section>
      </div>
    </div>
  </div>
</article></main>
<div class="m-doc-search" id="search">
  <a href="#!" onclick="return hideSearch()"></a>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-m-8 m-push-m-2">
        <div class="m-doc-search-header m-text m-small">
          <div><span class="m-label m-default">Tab</span> / <span class="m-label m-default">T</span> to search, <span class="m-label m-default">Esc</span> to close</div>
          <div id="search-symbolcount">&hellip;</div>
        </div>
        <div class="m-doc-search-content">
          <form>
            <input type="search" name="q" id="search-input" placeholder="Loading &hellip;" disabled="disabled" autofocus="autofocus" autocomplete="off" spellcheck="false" />
          </form>
          <noscript class="m-text m-danger m-text-center">Unlike everything else in the docs, the search functionality <em>requires</em> JavaScript.</noscript>
          <div id="search-help" class="m-text m-dim m-text-center">
            <p class="m-noindent">Search for symbols, directories, files, pages or
            modules. You can omit any prefix from the symbol or file path; adding a
            <code>:</code> or <code>/</code> suffix lists all members of given symbol or
            directory.</p>
            <p class="m-noindent">Use <span class="m-label m-dim">&darr;</span>
            / <span class="m-label m-dim">&uarr;</span> to navigate through the list,
            <span class="m-label m-dim">Enter</span> to go.
            <span class="m-label m-dim">Tab</span> autocompletes common prefix, you can
            copy a link to the result using <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">L</span> while <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">M</span> produces a Markdown link.</p>
          </div>
          <div id="search-notfound" class="m-text m-warning m-text-center">Sorry, nothing was found.</div>
          <ul id="search-results"></ul>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="search-v2.js"></script>
<script src="searchdata-v2.js" async="async"></script>
<footer><nav>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018&ndash;2024.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.9.6 and <a href="https://mcss.mosra.cz/">m.css</a>.</p>
      </div>
    </div>
  </div>
</nav></footer>
</body>
</html>
